﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Media3D;

namespace TimeSolve
{
   public static class VectorCal
    {
        public static bool Vector2Parse(string str,out double x,out  double y)
        {
            str = str.Replace('，', ',');
            string[] s = str.Split(',');
            if (s.Length == 2)
            {
                if (double.TryParse(s[0], out x) && double.TryParse(s[1], out y))
                {
                    return true;
                }
            }
            x = 0;y = 0;
            return false;
        }
        public static bool Vector3Parse(string str, out double x, out double y,out double z)
        {
            str = str.Replace('，', ',');
            string[] s = str.Split(',');
            if (s.Length == 3)
            {
                if (double.TryParse(s[0], out x) && double.TryParse(s[1], out y) && double.TryParse(s[2], out z))
                {
                    return true;
                }
            }
            x = 0; y = 0;z = 0;
            return false;
        }
        public static Vector3D Cross(Vector3D a, Vector3D b)
        {
            return new Vector3D(a.Y * b.Z - a.Z * b.Y, a.Z * b.X - a.X * b.Z, a.X * b.Y - a.Y * b.X);
        }
        public static double  Point(Vector3D a, Vector3D b)
        {
            return a.X * b.X + a.Y * b.Y + a.Z * b.Z;
        }
        public static Point3D PointRotate(Point3D Old, Vector3D axis_1, Point3D pAxis, double Radian)
        {//右手坐标系下，vector朝向你，逆时针旋转
            double c = Math.Cos(Radian);
            double c1 = 1 - c;
            double s = Math.Sin(Radian);
            double axy = axis_1.X * axis_1.Y;
            double ayz = axis_1.Y * axis_1.Z;
            double axz = axis_1.X * axis_1.Z;
            return new Point3D(
                (axis_1.X * axis_1.X * c1 + c) * (Old.X - pAxis.X) + (axy* c1 - axis_1.Z * s) * (Old.Y - pAxis.Y) + (axz * c1 + axis_1.Y * s) * (Old.Z - pAxis.Z) + pAxis.X,
                (axy * c1+ axis_1.Z * s) * (Old.X - pAxis.X) + (axis_1.Y * axis_1.Y * c1 + c) * (Old.Y - pAxis.Y) + (ayz * c1 - axis_1.X * s) * (Old.Z - pAxis.Z) + pAxis.Y,
                 (axz * c1 - axis_1.Y * s) * (Old.X - pAxis.X) + (ayz * c1 + axis_1.X * s) * (Old.Y - pAxis.Y) + (axis_1.Z * axis_1.Z * c1 + c) * (Old.Z - pAxis.Z) + pAxis.Z
                );
        }
        public static Point3D PointToPlane(Point3D pointOut, Vector3D vectorOfPlane, Point3D pointInPlane)
        {
            double t = (vectorOfPlane.X * (pointOut.X - pointInPlane.X)
                + vectorOfPlane.Y * (pointOut.Y - pointInPlane.Y)
                + vectorOfPlane.Z * (pointOut.Z - pointInPlane.Z))
                / (vectorOfPlane.LengthSquared);
            return new Point3D(pointOut.X - vectorOfPlane.X * t,
                pointOut.Y - vectorOfPlane.Y * t, pointOut.Z - vectorOfPlane.Z * t);
        }
        public static Vector3D VectorToPlane(Vector3D Vector, Vector3D vectorOfPlane)
        {//矢量在平面上的投影
    
            return Vector - Vector3D.DotProduct(Vector, vectorOfPlane) / vectorOfPlane.LengthSquared * vectorOfPlane;
        }

    }
}


//return new Point3D(
//    (axis_1.X * axis_1.X * (1 - c) + c) * (Old.X - pInAxis.X) + (axis_1.X * axis_1.Y * (1 - c) - axis_1.Z * s) * (Old.Y - pInAxis.Y) + (axis_1.X * axis_1.Z * (1 - c) + axis_1.Y * s) * (Old.Z - pInAxis.Z) + pInAxis.X,
//    (axis_1.Y * axis_1.X * (1 - c) + axis_1.Z * s) * (Old.X - pInAxis.X) + (axis_1.Y * axis_1.Y * (1 - c) + c) * (Old.Y - pInAxis.Y) + (axis_1.Y * axis_1.Z * (1 - c) - axis_1.X * s) * (Old.Z - pInAxis.Z) + pInAxis.Y,
//     (axis_1.X * axis_1.Z * (1 - c) - axis_1.Y * s) * (Old.X - pInAxis.X) + (axis_1.Y * axis_1.Z * (1 - c) + axis_1.X * s) * (Old.Y - pInAxis.Y) + (axis_1.Z * axis_1.Z * (1 - c) + c) * (Old.Z - pInAxis.Z) + pInAxis.Z
//    );